package ch.simplix.android.repetitive.service.implementation;

import java.util.concurrent.TimeUnit;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.simplix.android.repetitive.definition.RepeaterAction;
import ch.simplix.android.repetitive.implementation.RepetitiveContextFacade;

/**
 * This class implements the possibility to process repetitive tasks via {@link RepetitiveContextFacade}.
 * 
 * @author SimpliX
 * 
 */
public abstract class ARepetitiveService extends Service implements
		RepeaterAction {

	private RepetitiveContextFacade repeaterContext;
	
	public abstract void onStart(Intent intent);
	public abstract void onRepeat();
	public abstract void onStop();
	
	public boolean isRunning() {
		return repeaterContext != null && repeaterContext.isRunning();
	}

	public ARepetitiveService() {
		super();		
		repeaterContext = new RepetitiveContextFacade(this);

	}

	@Override
	public void onDestroy() {
		if(repeaterContext==null) return;
		repeaterContext.stopTimedRepetition();
		repeaterContext.finalize();
		repeaterContext = null;
		super.onDestroy();

	}

	/**
	 * Start repeating the action- after the time specified
	 */
	protected void startTimedRepetition(Intent intent) {
		if (repeaterContext == null)
			return;		
		onStart(intent);
		repeaterContext.startTimedRepetition();
	}

	/**
	 * Stop repeating the action.
	 */
	protected void stopTimedRepetition() {
		if (repeaterContext == null)
			return;
		repeaterContext.stopTimedRepetition();
		onStop();		
	}	

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {	
		super.onStartCommand(intent, flags, startId);
		if (intent == null || intent.getExtras() == null)
		{	// mark this as non sticky
			// Means: Don't restart the service if there are no
			// pending intents.
			return Service.START_NOT_STICKY;
		}
		long interval = intent.getExtras().getInt("intervalInDays");
		interval = interval * 24 + intent.getExtras().getInt("intervalInHours");
		interval = interval * 60
				+ intent.getExtras().getInt("intervalInMinutes");
		interval = interval * 60
				+ intent.getExtras().getInt("intervalInSeconds");
		interval = interval * 1000
				+ intent.getExtras().getInt("intervalInMilliSeconds");
		if (interval > 0) {
			this.setUpdateInterval(interval, TimeUnit.MILLISECONDS);
		}
		
		boolean start = intent.getExtras().getBoolean("start");
		if (start) {
			startTimedRepetition(intent);
		}
		boolean stop = intent.getExtras().getBoolean("stop");
		if (stop) {			
			this.stopTimedRepetition();
		}
		
		// mark this as non sticky
		// Means: Don't restart the service if there are no
		// pending intents.
		return Service.START_NOT_STICKY;
	}

	@Override
	public Context getContext() {
		return this;
	}

	public void setUpdateInterval(long updateInterval, TimeUnit unit) {
		if (repeaterContext == null)
			return;
		Log.d("ARepetitiveService", "updateInterval:"+updateInterval);		
		repeaterContext.setUpdateInterval(updateInterval, unit);
	}

	public long getUpdateIntervalInMillis() {
		if (repeaterContext == null)
			return Long.MAX_VALUE;
		return repeaterContext.getUpdateIntervalInMillis();
	}

}